/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kaytto1;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;

/**
 *
 * @author Joteitti
 */
public class ImageForm extends Form {

    private HomeService homeService;
    private TabbedPane imageTabbedPane;

    public ImageForm(HomeService homeService) {
        super();

        // Set home service
        this.homeService = homeService;

        // Create form layout and components
        setTitle(Text.getString(Text.TEXT_IMAGE_TITLE));
        addCommandListener(homeService);
        addCommand(homeService.getContinueCommand());
        addCommand(homeService.getBackCommand());
        setTransitionOutAnimator(homeService.getTransition());
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addComponent(getImageTabbedPane());
    }

    public void createImageTab(Image image) {
        // Cratea thumbail
        int thumbailWidth = getPreferredW();
        Image thumbail = Utilities.createThumbail(image, thumbailWidth, -1);

        // Create components
        Container container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        Label imageLabel = new Label(thumbail);
        imageLabel.setAlignment(Component.CENTER);
        container.addComponent(imageLabel);
        Label descriptionLabel = new Label(Text.getString(Text.TEXT_IMAGE_DESCRIPTION));
        container.addComponent(descriptionLabel);
        TextArea imageTextArea = new TextArea();
        imageTextArea.setSingleLineTextArea(false);
        imageTextArea.setEditable(true);
        imageTextArea.setFocusable(true);
        imageTextArea.setRows(2);
        container.addComponent(imageTextArea);
        Button removeButton = new Button(new Command(Text.getString(Text.TEXT_IMAGE_REMOVE_PICTURE)) {

            public void actionPerformed(ActionEvent evt) {
                // Remove current tab
                int index = imageTabbedPane.getSelectedIndex();
                if(index != -1) {
                    imageTabbedPane.removeTabAt(index);
                    
                    // Set selected index
                    if(imageTabbedPane.getTabCount() != 0)
                        imageTabbedPane.setSelectedIndex(0);

                    // Update form
                    revalidate();
                    repaint();
                }
            }

        });
        removeButton.setAlignment(Component.CENTER);
        container.addComponent(removeButton);

        // Add to tabs
        String imageTitle = Text.getString(Text.TEXT_GLOBAL_PICTURE) + Integer.toString(imageTabbedPane.getTabCount());
        imageTabbedPane.addTab(imageTitle, container);
    }

    public boolean continueable() {
        if(imageTabbedPane.getTabCount() != 0) {
            return true;
        }

        return false;
    }

    public int getImageCount() {
        return imageTabbedPane.getTabCount();
    }

    public void clearImages() {
        imageTabbedPane.removeAll();
    }

    private TabbedPane getImageTabbedPane() {
        // Lazy initialization
        if(imageTabbedPane == null) {
            imageTabbedPane = new TabbedPane(Component.TOP);
        }

        return imageTabbedPane;
    }
    
}
